package com.lantosec.hmrp.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantosec.hmrp.core.config.InitDataListener;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.util.UUIDUtils;
import com.lantosec.hmrp.modules.system.entity.UserAutograph;
import com.lantosec.hmrp.modules.system.mapper.UserAutographMapper;
import com.lantosec.hmrp.modules.system.service.IUserAutographService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.modules.system.vo.UserAutographVO;
import com.lantosec.hmrp.modules.uploads.entity.SysUploads;
import com.lantosec.hmrp.modules.uploads.service.ISysUploadsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.context.annotation.Primary;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-06-28
 */
@Service
@Primary
@Log4j2
public class UserAutographServiceImpl extends ServiceImpl<UserAutographMapper, UserAutograph> implements IUserAutographService {
    @Autowired
    private ISysUploadsService iSysUploadsService;

    @Override
    public UserAutographVO getByUserId(String userId) {
        UserAutograph userAutograph = this.getOne(new QueryWrapper<UserAutograph>().eq(UserAutograph.ColName.user_id.toString(), userId).eq(UserAutograph.ColName.main_flag.toString(), 1));
        if (Objects.isNull(userAutograph)) {
            return null;
        }
        UserAutographVO userAutographVO = this.getUserAutographVo(userAutograph);
        return userAutographVO;
    }

    @Override
    public Result saveUserAutograph(UserAutographVO vo) {
        UserAutograph entity = vo.convertToEntity();
        //判断用户签名是否已存在主签名
        Integer mainFlag = entity.getMainFlag();
        if (mainFlag == 1) {
            String userId = entity.getUserId();
            UserAutograph userAutograph = this.getOne(new QueryWrapper<UserAutograph>().eq(UserAutograph.ColName.user_id.toString(), userId)
                                                                                        .eq(UserAutograph.ColName.main_flag.toString(), 1));
            if(Objects.nonNull(userAutograph)){
                return Result.ok("当前用户已存在主签名",false);
            }else {
                entity.setUserInfoAutographId(UUIDUtils.get());
                this.save(entity);
                return Result.ok("当前用户不存在主签名",true);
            }
        }else {
            entity.setUserInfoAutographId(UUIDUtils.get());
            this.save(entity);
            return Result.ok("当前用户不存在主签名",true);
        }
    }

    @Override
    public List<UserAutographVO> getAll(String userId) {
        List<UserAutograph> userAutographs = this.list(new QueryWrapper<UserAutograph>().eq(UserAutograph.ColName.user_id.toString(), userId));
        List<UserAutographVO> userAutographVOs = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(userAutographs)){
            userAutographs.forEach(param->{
                UserAutographVO userAutographVO = getUserAutographVo(param);
                userAutographVOs.add(userAutographVO);
            });
        }
        return userAutographVOs;
    }

    private UserAutographVO getUserAutographVo(UserAutograph param) {
        UserAutographVO userAutographVO = param.convertToVO();
        String savePath = null;
        try {
            String uploadId = param.getUploadId();
            SysUploads sysUploads = iSysUploadsService.getById(uploadId);
            savePath = InitDataListener.uploadsUrl + sysUploads.getSavePath() + sysUploads.getFileName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        userAutographVO.setSavePath(savePath);
        return userAutographVO;
    }
}
